package com.xnfh.home.pop

import android.app.Activity
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.impl.PartShadowPopupView
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.bean.CropTypeBean
import com.xnfh.framework.bean.RespStateData
import com.xnfh.framework.utils.getStringFromResource
import com.xnfh.home.R
import com.xnfh.home.adapter.DiseaseTypeAdapter
import com.xnfh.home.databinding.PopDiseaseTypeBinding

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2024/1/5 18:48
 * @UpdateUser: ziheng
 * @UpdateDate: 2024/1/5 18:48
 * @UpdateRemark: 更新说明
 */
class DiseaseTypePop(mContext: Context, val cropList: List<CropTypeBean>?) : PartShadowPopupView(mContext) {

    override fun getImplLayoutId(): Int = R.layout.pop_disease_type

    private lateinit var listener: onListener
    private lateinit var adapter: DiseaseTypeAdapter

    //选中的数据
    private var cropName: String? = null
    private var cropId: Int? = null
    override fun onCreate() {
        super.onCreate()
        val mBind = DataBindingUtil.bind<PopDiseaseTypeBinding>(popupImplView)
        mBind?.popDiseaseRecycle?.layoutManager = LinearLayoutManager(context)
        adapter = DiseaseTypeAdapter()
        adapter.addData(0, CropTypeBean(getStringFromResource(R.string.str_all),-1,false))
        cropList?.let {
            adapter.addData(it)
        }
        adapter.setOnItemClickListener { adapter, view, position ->
            this.adapter.updateSelectItem(position)
            if(position == 0){
                cropName = null
                cropId = null
                return@setOnItemClickListener
            }
            val cropTypeBean = adapter.data[position] as CropTypeBean
            cropName = cropTypeBean.typeName
            cropId = cropTypeBean.id
        }
        mBind?.popDiseaseRecycle?.adapter = adapter
        mBind?.popDiseaseSure?.setOnClickListener{
            listener.onSure(cropName,cropId)
        }
        mBind?.popDiseaseReset?.setOnClickListener{
            cropName = null
            cropId = null
            adapter.resetSelect()
        }
    }

    interface onListener{
        fun onSure(cropName: String?,cropId: Int?)
    }

    fun setOnListener(listener: onListener){
        this.listener = listener
    }

}